<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="器具型号" prop="materialCode">
        <el-input
          v-model="queryParams.materialCode"
          placeholder="请输入器具型号编码"
          clearable
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所在库房" prop="currentWarehouseId">
        <el-select
          v-model="queryParams.currentWarehouseId"
          placeholder="请选择库房"
          clearable
          style="width: 200px"
        >
          <el-option
            v-for="warehouse in warehouseList"
            :key="warehouse.warehouseId"
            :label="warehouse.warehouseName"
            :value="warehouse.warehouseId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属项目" prop="currentProjectId">
        <el-select
          v-model="queryParams.currentProjectId"
          placeholder="请选择项目"
          clearable
          style="width: 200px"
        >
          <el-option
            v-for="project in projectList"
            :key="project.projectId"
            :label="project.projectName"
            :value="project.projectId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="器具状态" prop="locationStatus">
        <el-select
          v-model="queryParams.locationStatus"
          placeholder="请选择状态"
          clearable
          style="width: 120px"
        >
          <el-option label="在库" value="1" />
          <el-option label="在途" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="handleRefresh"
        >刷新</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 汇总统计表格 -->
    <SummaryTable
      :summary-list="summaryList"
      :loading="loading"
      :total="total"
      :query-params="queryParams"
      @detail-click="handleDetailClick"
      @sort-change="handleSortChange"
      @pagination="getList"
    />

    <!-- 详情抽屉 -->
    <ContainerDetailDrawer
      :visible.sync="detailVisible"
      :summary-info="selectedSummary"
    />
  </div>
</template>

<script>
import {
  getContainerSummary
} from '@/api/pack/container'
import { listWarehouse } from '@/api/pack/warehouse'
import { listProject } from '@/api/pack/project'
import SummaryTable from './components/SummaryTable.vue'
import ContainerDetailDrawer from './components/ContainerDetailDrawer.vue'

export default {
  name: 'ContainerSummary',
  components: {
    SummaryTable,
    ContainerDetailDrawer
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 汇总数据列表
      summaryList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialCode: null,
        currentWarehouseId: null,
        currentProjectId: null,
        locationStatus: null,
        orderByColumn: null,
        isAsc: null
      },
      // 仓库列表
      warehouseList: [],
      // 项目列表
      projectList: [],
      // 详情抽屉相关
      detailVisible: false,
      selectedSummary: {}
    }
  },
  created() {
    this.getList()
    this.getWarehouseList()
    this.getProjectList()
  },
  methods: {
    /** 查询汇总列表 */
    getList() {
      this.loading = true
      getContainerSummary(this.queryParams).then(response => {
        this.summaryList = response.rows || []
        this.total = response.total || 0
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    /** 查询仓库列表 */
    getWarehouseList() {
      listWarehouse({ status: '0' }).then(response => {
        this.warehouseList = response.rows || []
      }).catch(error => {
        console.error('仓库列表加载失败:', error)
        this.$modal.msgError('仓库列表加载失败')
      })
    },
    /** 查询项目列表 */
    getProjectList() {
      listProject({ projectStatus: '1' }).then(response => {
        this.projectList = response.rows || []
      }).catch(error => {
        console.error('项目列表加载失败:', error)
        this.$modal.msgError('项目列表加载失败')
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryParams.orderByColumn = null
      this.queryParams.isAsc = null
      this.handleQuery()
    },
    /** 刷新按钮操作 */
    handleRefresh() {
      this.getList()
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pack/container/summary/export', {
        ...this.queryParams
      }, `container_summary_${new Date().getTime()}.xlsx`)
    },
    /** 详情点击处理 */
    handleDetailClick(row) {
      this.selectedSummary = { ...row }
      this.detailVisible = true
    },
    /** 排序变化处理 */
    handleSortChange({ column, prop, order }) {
      this.queryParams.orderByColumn = prop
      this.queryParams.isAsc = order === 'ascending' ? 'asc' : 'desc'
      this.getList()
    }
  }
}
</script>

<style scoped>
/* 器具汇总统计页面样式 */
</style>
